package com.alison.SmartCli;
import com.alison.SmartCli.Utils.SimpleLogger;
import java.util.Scanner;

public class SmartCli {
    private MethodManager methodManager;

    public SmartCli(){
        this.methodManager = new MethodManager();
    }

    /**
     * 默认关闭debug
     * @param debug
     */
    public void setDebug(boolean debug){
        SimpleLogger.setDebug(debug);
    }

    public void scanPackage(String packageName){
        Bootstrap.handlePackage(packageName);
    }

    public void setController(Class clazz) {
        Bootstrap.handleClass(clazz);
    }

    public void handle(String[] args){
        if(args.length == 0){
            System.out.println("show All usage");
            return;
        }
        String methodName = args[0];
        if (isInteractive(methodName)) {
            interactive();
        } else {
            handleInput(args);
        }
    }

    private void handleInput(String[] args) {
        if (args.length == 0) return;
        String methodName = args[0];
        String[] params = new String[args.length - 1];
        for (int i = 1; i <= args.length - 1; i++) {
            params[i - 1] = args[i];
        }
        methodManager.invoke(methodName, params);
    }

    private void interactive() {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.print("\u001b[4C");
            System.out.print(">>>");
            String line = scanner.nextLine();
            String[] args = line.trim().split("\\s+");
            handleInput(args);
        }
    }

    private boolean isInteractive(String methodName) {
        return methodName.toLowerCase().equals("--i");
    }



}
